package com.threeti.jiayusms.framework.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * String Utils
 * 
 * @author ben.z,snakeskin
 * 
 */
public class StringUtil {

	/**
	 * get Exception Stack Trace
	 * 
	 * @param ex
	 * @return
	 */
	public static String getStackTrace(Exception ex) {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		ex.printStackTrace(new PrintStream(outStream));
		return new String(outStream.toByteArray());
	}

	/**
	 * 
	 * @param string
	 * @param num
	 * @return
	 */
	public static String parseLongString(String string, int num) {
		if (string != null) {
			int count = string.length() / num;
			int mod = string.length() % num;
			for (int j = 1; j <= count; j++) {
				string = string.substring(0, j * num + (j - 1) * 4)
						+ "<br>"
						+ string.substring(j * num + (j - 1) * 4, string
								.length());
			}
			if (mod == 0) {
				string = string.substring(0, string.length() - 4);
			}
		}
		return string;
	}

	// if "str" isn't Long type then return null,otherwise return Long value;
	public static Long parseString2Long(String str) {
		Long value = null;
		if (str != null) {
			if (!str.trim().equals("")) {
				if (isNumber(str)) {
					if (checkLongLength(str)) {
						value = Long.parseLong(str);
					} else {
						value = null;
					}
				} else {
					value = null;
				}
			}
		}
		return value;
	}

	public static Double parseString2Double(String str) {
		Double value = null;
		if (str != null) {
			if (!str.trim().equals("")) {
				try {
					value = Double.parseDouble(str);
				} catch (Exception ex) {

				}
			}
		}
		return value;
	}
	
	public static BigDecimal parseString2BigDecimal(String str) {
		BigDecimal value = null;
		if (str != null) {
			if (!str.trim().equals("")) {
				try {
					value = BigDecimal.valueOf(Double.parseDouble(str));
				} catch (Exception ex) {

				}
			}
		}
		return value;
	}

	// if "str" isn't Integer type then return null,otherwise return Integer
	// value;
	public static Integer parseString2Integer(String str) {
		Integer value = null;
		if (str != null) {
			if (!str.trim().equals("")) {
				if (isNumber(str)) {
					if (checkIntegerLength(str)) {
						value = Integer.parseInt(str);
					} else {
						value = null;
					}
				} else {
					value = null;
				}
			}
		}
		return value;
	}

	// if "str" isn't Short type then return null,otherwise return Short value;
	public static Short parseString2Short(String str) {
		Short value = null;
		if (str != null) {
			if (!str.trim().equals("")) {
				if (isNumber(str)) {
					if (checkShortLength(str)) {
						value = Short.parseShort(str);
					} else {
						value = null;
					}
				} else {
					value = null;
				}
			}
		}
		return value;
	}

	public final static String DOUBLE_PATTERN_OF_REG_EXPRESSION = "[-|+]?\\d+([.]\\d+)?";

	/**
	 * check string empty
	 * 
	 * @param str
	 * 
	 * @return boolean true, false
	 */
	public static boolean isEmpty(String str) {
		if (str == null || "".equals(str.trim())) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isEmptySpace(String str) {
		if (str == null || "".equals(str)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * check string length
	 * 
	 * @param str
	 * @param len
	 * @return boolean true,false
	 */
	public static boolean checkLength(String str, int len) {
		int length = 0;
		if (str != null) {
			try {
				length = str.getBytes("UTF-8").length;
			} catch (UnsupportedEncodingException uee) {
			}
		}
		if (len < length) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * check string number
	 * 
	 * @param str
	 * @return boolean true,false
	 */
	public static boolean isNumber(String str) {
		if (str != null && str.matches("\\d+")) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Check specified str, format like this:"GMT+23:59"
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isTimeZone(String str) {
		if (str != null
				&& str
						.matches("GMT[+|-](([0-9])|(0[0-9])|(1[0-9])|(2[0-3])):([0-5][0-9])")) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * @param str
	 * @return
	 */
	public static boolean checkLongLength(String str) {
		BigInteger bi = new BigInteger(str);
		if (bi.compareTo(new BigInteger(String.valueOf(Long.MAX_VALUE))) > 0) {
			return false;
		} else {
			return true;
		}
	}

	public static boolean checkDoubleLength(String str) {
		Double bi = new Double(str);
		if (bi.compareTo(new Double(String.valueOf(Double.MAX_VALUE))) > 0) {
			return false;
		} else {
			return true;
		}
	}

	public static boolean checkIntegerLength(String str) {
		Integer bi = new Integer(str);
		if (bi.compareTo(new Integer(String.valueOf(Integer.MAX_VALUE))) > 0) {
			return false;
		} else {
			return true;
		}
	}

	public static boolean checkShortLength(String str) {
		Short bi = new Short(str);
		if (bi.compareTo(new Short(String.valueOf(Short.MAX_VALUE))) > 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * check string double
	 * 
	 * @param str
	 * @return boolean true,false
	 */
	public static boolean isDouble(String str) {
		if (str != null && str.matches(DOUBLE_PATTERN_OF_REG_EXPRESSION)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * check string date
	 * 
	 * @param str
	 * @param format
	 * @return
	 */
	public static boolean isDate(String str, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			sdf.setLenient(true);
			sdf.parse(str);
			return true;
		} catch (ParseException e) {
			return false;
		}
	}
	
	/**
	 * Input stream transfer to string
	 * @param in
	 * @return
	 * @throws IOException 
	 */
	public static String stream2String(InputStream in) throws IOException{
		if(in == null){
			return null;
		}
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		StringBuilder sb = new StringBuilder();
		String line = null;
		while ((line = br.readLine()) != null) {
			sb.append(line + "\n");
		}
		return sb.toString();
	}

	public static Long string2Long(String str){
		if(str == null){
			return null;
		}
		return Long.valueOf(str);
	}
	
	public static BigDecimal string2BigDecimal(String str){
		if(str == null){
			return null;
		}
		return new BigDecimal(str).setScale(2, BigDecimal.ROUND_HALF_UP);
	}
	
	public static BigDecimal string2BigDecimal(String str, int scale){
		if(str == null){
			return null;
		}
		return new BigDecimal(str).setScale(scale, BigDecimal.ROUND_HALF_UP);
	}
	
	public static BigDecimal string2BigDecimal(String str, int scale, int roundingMode){
		if(str == null){
			return null;
		}
		return new BigDecimal(str).setScale(scale, roundingMode);
	}
	
	public static Short string2Short(String str){
		if(str == null){
			return null;
		}
		return Short.valueOf(str);
	}
	
	public static Integer string2Integer(String str){
		if(str == null){
			return null;
		}
		return Integer.valueOf(str);
	}
}
